package com.costner.kids;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.AutoParallaxBackground;
import org.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends BaseGameActivity {

	private Camera camera;
	private BitmapTextureAtlas mAutoParallaxBackgroundTexture;
	private ITextureRegion mParallaxLayerFront;
	private ITextureRegion mParallaxLayerBack;
	private ITextureRegion mParallaxLayerMid;
	private BitmapTextureAtlas Numbers;
	private ITextureRegion NumbersRegion;
	private BitmapTextureAtlas Letters;
	private ITextureRegion LettersRegion;
	private BitmapTextureAtlas Pictures;
	private ITextureRegion PicturesRegion;
	private BitmapTextureAtlas Statistics;
	private ITextureRegion StatisticsRegion;
	private Scene scene;
	private SceneManager sceneManager;
	
	
	
	

	@Override
	public EngineOptions onCreateEngineOptions() {
		camera = new Camera(0, 0, Constants.CAMERA_WIDTH, Constants.CAMERA_HEIGHT);
		
		final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(Constants.CAMERA_WIDTH, Constants.CAMERA_HEIGHT), camera);
		engineOptions.getAudioOptions().setNeedsSound(true);

		return engineOptions;
	}

	@Override
	public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {
		sceneManager = new SceneManager(this, mEngine, camera);
		sceneManager.loadSplashSceneResources();
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		this.mAutoParallaxBackgroundTexture = new BitmapTextureAtlas(this.getTextureManager(), 1024, 1024);
		this.mParallaxLayerFront = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "pbg-front.png", 0, 0);
		this.mParallaxLayerBack = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "pbg-back.png", 0, 99);
		this.mParallaxLayerMid = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "pbg-mid.png", 0, 580);
		this.mAutoParallaxBackgroundTexture.load();
		
		Numbers = new BitmapTextureAtlas(this.getTextureManager(), 50, 50, TextureOptions.BILINEAR);
		NumbersRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(Numbers, this, "ic_numbersactivity.png", 0, 0);
		Numbers.load();
		
		Letters = new BitmapTextureAtlas(this.getTextureManager(), 50, 50, TextureOptions.BILINEAR);
		LettersRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(Letters, this, "ic_lettersactivity.png", 0, 0);
		Letters.load();
		
		Pictures = new BitmapTextureAtlas(this.getTextureManager(), 50, 50, TextureOptions.BILINEAR);
		PicturesRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(Pictures, this, "ic_picturesactivity.png", 0, 0);
		Pictures.load();
		
		Statistics = new BitmapTextureAtlas(this.getTextureManager(), 50, 50, TextureOptions.BILINEAR);
		StatisticsRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(Statistics, this, "ic_statisticsactivity.png", 0, 0);
		Statistics.load();
		
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception {

		scene = new Scene();
		//Parallel bg
		final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(0, 0, 0, 1);
		final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
		autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(0.0f, new Sprite(0, Constants.CAMERA_HEIGHT - this.mParallaxLayerBack.getHeight(), this.mParallaxLayerBack, vertexBufferObjectManager)));
		autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-5.0f, new Sprite(0, 80, this.mParallaxLayerMid, vertexBufferObjectManager)));
		autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-10.0f, new Sprite(0, Constants.CAMERA_HEIGHT - this.mParallaxLayerFront.getHeight(), this.mParallaxLayerFront, vertexBufferObjectManager)));
		scene.setBackground(autoParallaxBackground);
		
		final Sprite sprNumbers = new Sprite(Constants.X_POSITION_LEFT, Constants.Y_POSITION_TOP, this.NumbersRegion, this.getVertexBufferObjectManager()) {
        	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
        		if(pSceneTouchEvent.isActionDown()) {
        			startActivity(new Intent(MainActivity.this, NumbersActivity.class));
        			MainActivity.this.finish();
        		}
				return true;
        	}
        }; 
        
        final Sprite sprLetters = new Sprite(Constants.X_POSITION_MIDDLE, Constants.Y_POSITION_TOP, this.LettersRegion, this.getVertexBufferObjectManager()) {
        	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
        		if(pSceneTouchEvent.isActionDown()) {
        			startActivity(new Intent(MainActivity.this, LettersActivity.class));
        			MainActivity.this.finish();
        		}
				return true;
        	}
        };
        
        final Sprite sprPictures = new Sprite(Constants.X_POSITION_RIGHT, Constants.Y_POSITION_TOP, this.PicturesRegion, this.getVertexBufferObjectManager()) {
        	/*public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
        		if(pSceneTouchEvent.isActionDown()) {
        			startActivity(new Intent(MainActivity.this, NumbersActivity.class));
        			MainActivity.this.finish();
        		}
				return true;
        	}*/
        };
        
        final Sprite sprStatistics = new Sprite(Constants.X_POSITION_RIGHT, Constants.Y_POSITION_BOTTOM, this.StatisticsRegion, this.getVertexBufferObjectManager()) {
        	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
        		if(pSceneTouchEvent.isActionDown()) {
        			startActivity(new Intent(MainActivity.this, StatisticsActivity.class));
        			MainActivity.this.finish();
        		}
				return true;
        	}
        };
        
        sprNumbers.setScale(2);
        scene.attachChild(sprNumbers);
        scene.registerTouchArea(sprNumbers);
        sprLetters.setScale(2);
        scene.attachChild(sprLetters);
        scene.registerTouchArea(sprLetters);
        sprPictures.setScale(2);
        scene.attachChild(sprPictures);
        scene.registerTouchArea(sprPictures);
        //sprStatistics.setScale(2);
        scene.attachChild(sprStatistics);
        scene.registerTouchArea(sprStatistics);
		
		scene.setTouchAreaBindingOnActionDownEnabled(true);
		
		
		//return scene;
	}

	

	@Override
	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
